package com.rockxin.cat.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.widget.AppCompatSpinner;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.orhanobut.logger.Logger;
import com.rockxin.cat.R;
import com.rockxin.cat.adapter.SpinnerAdapter;
import com.rockxin.cat.base.BaseActivity;
import com.rockxin.cat.base.User;
import com.rockxin.cat.bean.MyBankCardBean;
import com.rockxin.cat.network.CatNetWork;
import com.rockxin.cat.network.HttpResult;
import com.rockxin.cat.utils.MoneyHandlingUtils;
import com.rockxin.cat.utils.RegexUtils;
import com.rockxin.cat.utils.ToastUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscriber;

public class DepositActivity extends BaseActivity {

    @BindView(R.id.back_img)
    ImageView backImg;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.rechargeCard)
    EditText rechargeCard;
    @BindView(R.id.deposit)
    TextView deposit;
    @BindView(R.id.charge)
    TextView charge;
    @BindView(R.id.next)
    TextView next;
    @BindView(R.id.spinner)
    AppCompatSpinner spinner;
    @BindView(R.id.phoneNum)
    EditText phoneNum;
    @BindView(R.id.code)
    EditText code;
    @BindView(R.id.getCode)
    TextView getCode;

    private DepositActivity mDepositActivity;
    private SpinnerAdapter adapter;
    private CatNetWork catNetWork;
    private String bankId;

    @Override
    protected int setLayout() {
        return R.layout.activity_deposit;
    }

    @Override
    public void initData() {
        mDepositActivity = this;
        title.setText("提现");

        if (catNetWork == null) {
            catNetWork = new CatNetWork();
        }

        catNetWork.getMyBankList(User.getInstance().getToken(), "list", new Subscriber<HttpResult<List<MyBankCardBean>>>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(HttpResult<List<MyBankCardBean>> myBankCardBeanHttpResult) {
                if (myBankCardBeanHttpResult.getStatus() == 1) {
                    final List<String> strings = new ArrayList<>();
                    final List<MyBankCardBean> data = myBankCardBeanHttpResult.getData();
                    for (int i = 0; i < data.size(); i++) {
                        strings.add(data.get(i).getBank_name());
                    }
                    adapter = new SpinnerAdapter(strings);
                    spinner.setDropDownVerticalOffset(100);//设置选择微调的弹出窗口中像素的垂直偏移。在mode_dropdown唯一有效的
                    spinner.setAdapter(adapter);
                    bankId = data.get(0).getId();
                    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                        @Override
                        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                            bankId = data.get(position).getId();
                        }

                        @Override
                        public void onNothingSelected(AdapterView<?> parent) {

                        }
                    });

                }
            }
        });
    }

    @OnClick({R.id.back_img, R.id.next, R.id.deposit, R.id.getCode})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_img:
                finish();
                break;
            case R.id.next:   //提现
                if (User.getInstance().getUserInfo().getMoney() == 0) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(mDepositActivity);
                    builder.setTitle("提现提醒");
                    builder.setMessage("提现金额不足");
                    builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });

                    builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
                    builder.show();
                }

                if (!RegexUtils.isMobileExact(phoneNum.getText().toString())) {
                    ToastUtils.showShortToastSafe("请输入正确的手机号");
                    return;
                }

                if (TextUtils.isEmpty(phoneNum.getText().toString())) {
                    ToastUtils.showShortToastSafe("手机号不能为空");
                    return;
                }
                if (TextUtils.isEmpty(code.getText().toString())) {
                    ToastUtils.showShortToastSafe("验证码不能为空");
                    return;
                }


                HashMap<String, String> stringHashMap = new HashMap<>();
                stringHashMap.put("token", User.getInstance().getToken());
                stringHashMap.put("amount", rechargeCard.getText().toString());
                stringHashMap.put("code", code.getText().toString());
                stringHashMap.put("bid", bankId);
                catNetWork.withDraw(stringHashMap, new Subscriber<HttpResult>() {
                    @Override
                    public void onCompleted() {
                        Logger.e("onCompleted");

                    }

                    @Override
                    public void onError(Throwable e) {
                        Logger.e("onError");
                        e.printStackTrace();

                    }

                    @Override
                    public void onNext(HttpResult httpResult) {
                        if (httpResult.getStatus() == 1) {
                            Intent intent = new Intent(DepositActivity.this, WithdrawalsSuccessActivity.class);
                            intent.putExtra("fee", charge.getText().toString());
                            intent.putExtra("money", rechargeCard.getText().toString());
                            startActivity(intent);
                        }
                    }
                });
                break;

            case R.id.deposit:
                rechargeCard.setText(MoneyHandlingUtils.formatDouble(User.getInstance().getUserInfo().getMoney()));
                break;
            case R.id.getCode:
                timer.start();
                catNetWork.getDepositCode(User.getInstance().getToken(), phoneNum.getText().toString(), new Subscriber<HttpResult>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpResult httpResult) {
                        if (httpResult.getStatus() == 1) {
                            ToastUtils.showShortToastSafe(httpResult.getInfo());
                        }
                    }
                });
                break;
        }
    }

    private CountDownTimer timer = new CountDownTimer(60000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            getCode.setText((millisUntilFinished / 1000) + "秒后可重发");
            getCode.setEnabled(false);
        }

        @Override
        public void onFinish() {
            getCode.setEnabled(true);
            getCode.setText("获取验证码");
        }
    };
}
